<template>
  <el-dialog
    :title="info"
    append-to-body
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="800px">
    <el-form ref="form" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="所属银行" align="left" prop="bank">
        <el-select v-model="form.bank" placeholder="所属银行">
          <el-option
            v-for="(name,index) in bankList"
            :key="index"
            :label="name"
            :value="name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="卡号" align="left" prop="card">
        <el-input v-model="form.card" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" align="left" prop="realName">
        <el-input v-model="form.realName" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'CreateShopBankDialog',
  components: {},
  props:{
    successFunction:Function
  },
  data: function () {
    let $this=this
    var validateShopBank=function (rule, value, callback) {
      if (!$this.checkBankCard(value)){
        callback(new Error('银行卡 必须是由数字组成，长度(16,20)位之间'))
      }else{
        callback()
      }
    }
    return {
      info:'创建银行卡',
      dialogVisible: false,
      currLoading: false,
      bankList:dict.BankList,
      form: {
        card: '',
        realName: '',
        bank: dict.BankList[0],
      },
      rules: {
        card: [
          {required: true, message: '请输入 银行卡', trigger: ['blur', 'change']},
          {validator: validateShopBank, trigger: ['blur', 'change']}
        ],
        realName: [
          {required: true, message: '请输入 真实姓名', trigger: ['blur', 'change']},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: ['blur', 'change']}
        ],
        bank: [
          {required: true, message: '请选择 所属银行', trigger: ['blur', 'change']},
        ]
      }
    }
  },
  mounted () {
  },
  methods: {
    open: function () {
      this.form={
        realName: '',
        card: '',
        bank: dict.BankList[0],
        status: '1',
      }
      this.dialogVisible = true
      this.currLoading = false
      this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var form = Object.assign({}, $this.form)
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameShop + 'ShopBank/create', form, function () {
            $this.$message.success('操作成功!')
            $this.dialogVisible=false
            $this.currLoading = false
            $this.$props.successFunction()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
